<?php session_start(); ?>
<?php if(!isset($_SESSION["id"]))
     header("location:index.php");
?>

<html>
    <head>
        <title>Cong Thuc</title>
        <link href="public/css/css.css" rel="stylesheet" type="text/css">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
        <div id="wrapper">
            <div id="baner">

                

            </div><!---banber-->
             <nav>
             <ul id="s">
                  <li><a href="index.php">Home</a></li>
                  <li><a href="#">about us</a></li>
                  <li><a href="#">contact</a></li>
                  <li><a href="#">new</a></li>
                     
                <li>     <?php if (isset($_SESSION["id"])): ?>
                            <div id="logout"><a href="logout.php">LOGOUT</a></div>
                        <?php endif; ?></li>
             </ul>
       </nav>
            
        
<div id="col_left">


                <div class="logo">

<?php 
require_once 'model/works.php';
$works= new Works();
$work=$works->select_edit(null, $_GET['id']);
?>
<form method="post" action="upload.php?id=<?php echo $_GET["id"]?>"      enctype="multipart/form-data"> 
                <label for="file">Name:</label>
                <input type="text" name="name" value="<?php echo $work["name"]?>"><br>
                
                <div style="float: left; width: 10%;"><label for="altText">Description</label></div>
                        <div width: 60%;"><textarea name="des">
                            <?php echo $work["description"] ?>"</textarea></div>
                <div style="clear: both"></div>
                <label for="file">Filename:</label> 
                <div style="clear: both"></div>
                <input type="file" name="file" id="file"><br>
                <input type="submit" name="submit" value="Submit">  
            </form>
            
            <?php
if (isset($_POST['submit'])) {
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);
    if ((($_FILES["file"]["type"] == "image/gif")
            || ($_FILES["file"]["type"] == "image/jpeg")
            || ($_FILES["file"]["type"] == "image/jpg")
            || ($_FILES["file"]["type"] == "image/pjpeg")
            || ($_FILES["file"]["type"] == "image/x-png")
            || ($_FILES["file"]["type"] == "image/png"))
            && ($_FILES["file"]["size"] < 1000000)
            && in_array($extension, $allowedExts)) {
        if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        } else {
            echo "Upload: " . $_FILES["file"]["name"] . "<br>";
            echo "Type: " . $_FILES["file"]["type"] . "<br>";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

            if (file_exists("../public/imageupload/" . $_FILES["file"]["name"])) {
                echo "Error: ".$_FILES["file"]["name"] . " already exists. "."<br>";
            } else {
                move_uploaded_file($_FILES["file"]["tmp_name"], "public/imageupload/" . $_FILES["file"]["name"]);
                echo "Stored in: " . "public/imageupload/" . $_FILES["file"]["name"]."<br>";
                $array=array(
                    "id"=>$_GET["id"],
                    "name"=>$_POST["name"],
                    "des"=> $_POST["des"],
                    "img"=>"public/imageupload/" . $_FILES["file"]["name"]
                    );
                $works->update($array);
            }
            echo"Upload to: "."public/imageupload/". $_FILES["file"]["name"];
            $url="public/imageupload/".$_FILES["file"]["name"];
            echo "<img src='$url' with='200'/><br/>";
            
            
            
           
        }
    } else {
                        $array = array(
                                    "id" => $_GET["id"],
                                    "name" => $_POST["name"],
                                    "des" => $_POST["des"],
                                );
                                $works->update_noimg($array);
//                    echo "<img src='$img_url' width='100px' >";
    }
                }
                ?>
                <footer>
                <div id="boder">
                    <table class="o">
                        <tr>
                          
                            
                        </tr>
                    </table>
            </footer>
    </body>
</html>
